package com.ocom.webapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.ocom.common.entity.card.EntityXfMealTime;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.ConsTimeConfigOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.RandomUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.business.PayCenterService;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.service.PayConfigService;
import com.ocom.webapi.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PayConfigServiceImpl implements PayConfigService {

    @Autowired
    private PayConfigClientService payConfigClientService;


    @Autowired
    private PayCenterService payCenterService;

    @Autowired
    CompanyClientService companyClientService;
    /**
     * 餐段列表（MealNameMap/onShow）
     * */
    @Override
    public Result MealPageOnShow(Long comId) {

        Result result = payConfigClientService.GetMealNameInfo(comId);
        Gson gson = new Gson();
        String string = JSON.toJSONString(result.getData());
        MealNameMap mealNameMap = gson.fromJson(string, MealNameMap.class);
        List<MealNameVo> list = mealNameMap.getList();
        List<Map<String, Object> > mealpage = new ArrayList<>();
        for (MealNameVo mealNameVo: list) {
            Map<String, Object> map = new HashMap<>();
            ConsTimeConfigOnShowRequest request =new ConsTimeConfigOnShowRequest();
            request.setComId(comId);
            request.setMealNo(mealNameVo.getMealNo());
            request.setAccTypeId(0);
            request.setConsTimeZone(0);
            request.setPointId(0);
            request.setPage(0);
            request.setSize(0);
            Result result1 = payConfigClientService.GetMealTimeInfo(request);
            String str = JSON.toJSONString(result1.getData());
            MealTimeMap mealTimeMap = gson.fromJson(str, MealTimeMap.class);
            List<MealTimeVo> list1 = mealTimeMap.getList();
            map.put("mealId",mealNameVo.getId());
            map.put("mealNo",mealNameVo.getMealNo());
            map.put("mealName",mealNameVo.getMealName());
            map.put("comId",mealNameVo.getComId());
            map.put("createTime",mealNameVo.getCreateTime());

            if (list1.size() == 0){
                map.put("startTime",null);
                map.put("endTime",null);
            }else {
                for (MealTimeVo mealTimeVo: list1) {
                    map.put("id",mealTimeVo.getId());
                    map.put("startTime",mealTimeVo.getBeginTime());
                    map.put("endTime",mealTimeVo.getEndTime());
                    map.put("beforeHour",mealTimeVo.getBeforeHour());
                    map.put("allowDays",mealTimeVo.getAllowDays());
                    map.put("allowMinute",mealTimeVo.getAllowMinute());
                }
            }
            mealpage.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",mealpage.size());
        map.put("list",mealpage);
        return ResultUtil.success(map);
    }

    @Override
    public Result MealPageAdd(MealPageAddVo mealPageAddVo) {

        Map<String, Object> map = new HashMap<>();
        map.put("comId",mealPageAddVo.getComId());
        map.put("mealNo",mealPageAddVo.getMealNo());
        map.put("mealName",mealPageAddVo.getMealName());
        Result result = payConfigClientService.AddMealNameInfo(map);
        if(result.getCode()!=200){
            return ResultUtil.info(result.getCode(),result.getMsg());
        }
        EntityXfMealTime entityXfMealTime = new EntityXfMealTime();
        entityXfMealTime.setComId(mealPageAddVo.getComId());
        entityXfMealTime.setMealNo(mealPageAddVo.getMealNo());
        entityXfMealTime.setBeginTime(mealPageAddVo.getStartTime());
        entityXfMealTime.setEndTime(mealPageAddVo.getEndTime());
        entityXfMealTime.setCreateUser(SecurityUtils.getUsername());
        entityXfMealTime.setBeforeHour(mealPageAddVo.getBeforeHour());
        entityXfMealTime.setAllowDays(mealPageAddVo.getAllowDays());
        entityXfMealTime.setAllowMinute(mealPageAddVo.getAllowMinute());

        entityXfMealTime.setAccTypeId(0);
        entityXfMealTime.setPointId(0);
        entityXfMealTime.setConsTimeZone(0);
        Result result1 = payConfigClientService.AddMealTimeInfo(entityXfMealTime);
        if(result1.getCode()!=200){
            return ResultUtil.info(result1.getCode(),result.getMsg());
        }
        return payConfigClientService.GetMealNameInfo(mealPageAddVo.getComId());
    }

    /**
     * 餐段修改（MealPage/modify）
     * */
    @Override
    public Result MealPageModify(MealPageModifyVo mealPageModifyVo) {
        Map<String, Object> map = new HashMap<>();
        map.put("id",mealPageModifyVo.getMealId());
        map.put("comId",mealPageModifyVo.getComId());
        map.put("mealNo",mealPageModifyVo.getMealNo());
        map.put("mealName",mealPageModifyVo.getMealName());
        Result result = payConfigClientService.UpdateMealNameInfo(map);
        if(result.getCode()!=200){
            return ResultUtil.info(result.getCode(),result.getMsg());
        }
        EntityXfMealTime entityXfMealTime = new EntityXfMealTime();
        entityXfMealTime.setId(mealPageModifyVo.getId());
        entityXfMealTime.setComId(mealPageModifyVo.getComId());
        entityXfMealTime.setMealNo(mealPageModifyVo.getMealNo());
        entityXfMealTime.setBeginTime(mealPageModifyVo.getStartTime());
        entityXfMealTime.setEndTime(mealPageModifyVo.getEndTime());
        entityXfMealTime.setUpdateUser(SecurityUtils.getUsername());
        entityXfMealTime.setBeforeHour(mealPageModifyVo.getBeforeHour());
        entityXfMealTime.setAllowDays(mealPageModifyVo.getAllowDays());
        entityXfMealTime.setAllowMinute(mealPageModifyVo.getAllowMinute());
        entityXfMealTime.setAccTypeId(0);
        entityXfMealTime.setPointId(0);
        entityXfMealTime.setConsTimeZone(0);
        Result result1 = payConfigClientService.UpdateMealTimeInfo(entityXfMealTime);
        if(result1.getCode()!=200){
            return ResultUtil.info(result1.getCode(),result.getMsg());
        }
        ConsTimeConfigOnShowRequest request =new ConsTimeConfigOnShowRequest();
        request.setComId(mealPageModifyVo.getComId());
        request.setMealNo(mealPageModifyVo.getMealNo());
        request.setPage(0);
        request.setSize(0);
        return payConfigClientService.GetMealTimeInfo(request);
    }

    /**
     * 商户列表（MerchPage/onShow）
     * */
    @Override
    public Result MerchPageOnShow(MerchPageGetVo vo) {
        //GetMerchInfo中已获取单位名称com_name
        return payConfigClientService.GetMerchInfo(vo.getComId(),vo.getMerchName(),vo.getMerchType(),0,0,vo.getParentMerchId());
    }

    @Override
    public Result MerchPageInfo(Long comId, Integer merchId) {
        return payConfigClientService.MerchPageInfo(comId,merchId);
    }

    /**
     * 商户新增（MerchPage/add）
     * */
    @Override
    public Result MerchPageAdd(MerchPageAddVo merchPageAddVo) {
        //2.8.判断商户名称是否可用(getMerchInfoByName)
        Result result = payConfigClientService.GetMerchInfoByName(merchPageAddVo.getComId(), merchPageAddVo.getMerchName());
        if(result.getCode()!=200){
            return ResultUtil.error(ResultEnum.Err_3009);
        }
        merchPageAddVo.setUserName(RandomUtil.getRandomString(8));
        merchPageAddVo.setPassword(RandomUtil.getRandomString(8));

//        //2 统一支付平台发起请求
//        PayCenterCompanyVo payCenterCompanyVo=new PayCenterCompanyVo();
//        payCenterCompanyVo.setCompanyName(merchPageAddVo.getComId()+"#"+merchPageAddVo.getMerchName());
//        payCenterCompanyVo.setUserName(merchPageAddVo.getUserName());
//        payCenterCompanyVo.setPayPassword(merchPageAddVo.getPassword());
//        payCenterCompanyVo.setPayChannel("00000000000000000000");
//        payCenterCompanyVo.setIsEverySet(merchPageAddVo.getIsEverySet());
//        payCenterCompanyVo.setLocked(merchPageAddVo.getLocked());
//        payCenterCompanyVo.setContactName(merchPageAddVo.getContactName());
//        payCenterCompanyVo.setContactNumber(merchPageAddVo.getContactNumber());
//        payCenterCompanyVo.setContactAddress(merchPageAddVo.getContactAddress());
//
//        PayCenterVo payCenterVo=payCenterService.addMerch(payCenterCompanyVo);
//        if(payCenterVo==null || !payCenterVo.getCode().equals("0000")){
//            return ResultUtil.error(ResultEnum.Err_2018);
//        }
//        Map<String,String> map = JSON.parseObject(JSON.toJSONString(payCenterVo.getData()), Map.class);

        /**
         * TODO 这里请求通支付平台 成功后再本地插入
         */

        EntityXfMerchantInfo entityXfMerchantInfo = new EntityXfMerchantInfo();
        entityXfMerchantInfo.setComId(merchPageAddVo.getComId());
        entityXfMerchantInfo.setParentMerchId(merchPageAddVo.getParentMerchId().intValue());
        entityXfMerchantInfo.setRemark(merchPageAddVo.getRemark());
        entityXfMerchantInfo.setMerchName(merchPageAddVo.getMerchName());
//        entityXfMerchantInfo.setCreateUser(SecurityUtils.getUsername());
        entityXfMerchantInfo.setCreateUser("766");
        entityXfMerchantInfo.setWithholdSubmerch(merchPageAddVo.getWithholdSubmerch());

        //TODO 等统一平台返回
        entityXfMerchantInfo.setCompanyId("");
        entityXfMerchantInfo.setUserName(merchPageAddVo.getUserName());
        entityXfMerchantInfo.setPassword(merchPageAddVo.getPassword());

        entityXfMerchantInfo.setMerchType(merchPageAddVo.getMerchType());

        entityXfMerchantInfo.setMerchantUsername(merchPageAddVo.getMerchantUsername());
        entityXfMerchantInfo.setMerchantPhone(merchPageAddVo.getMerchantPhone());

        return payConfigClientService.AddMerchInfo(entityXfMerchantInfo);
    }

    /**
     * 商户修改（MerchPage/modify）
     * */
    @Override
    public Result MerchPageModify(MerchPageModifyVo merchPageModifyVo) {
        Result result = payConfigClientService.existMerchanName(merchPageModifyVo.getComId(),merchPageModifyVo.getMerchId(), merchPageModifyVo.getMerchName());
        if(result.getCode()!=200){
            return ResultUtil.info(result.getCode(),result.getMsg());
        }
        //1获取单位名称
        Result result2=companyClientService.GetCompanyListInfoOne(merchPageModifyVo.getComId());
        if(result2.getCode()!=200){
            return result2;
        }

//        EntityCompanyList entityCompanyList= JSONObject.parseObject(JSON.toJSONString(result2.getData()),EntityCompanyList.class);
        /**
         * 获取一条商户信息
         */
        Result result1 = payConfigClientService.MerchPageInfo(merchPageModifyVo.getComId(),merchPageModifyVo.getMerchId().intValue());
        if(result1.getCode()!=200){
            return result1;
        }
//        EntityXfMerchantInfo entityXfMerchantInfo1 = JSON.parseObject(JSON.toJSONString(result1.getData()),EntityXfMerchantInfo.class);

        EntityXfMerchantInfo entityXfMerchantInfo = new EntityXfMerchantInfo();
        entityXfMerchantInfo.setComId(merchPageModifyVo.getComId());
        entityXfMerchantInfo.setMerchId(merchPageModifyVo.getMerchId().intValue());
        entityXfMerchantInfo.setParentMerchId(merchPageModifyVo.getParentMerchId().intValue());
        entityXfMerchantInfo.setRemark(merchPageModifyVo.getRemark());
        entityXfMerchantInfo.setMerchName(merchPageModifyVo.getMerchName());
        entityXfMerchantInfo.setWithholdSubmerch(merchPageModifyVo.getWithholdSubmerch());
        entityXfMerchantInfo.setMerchType(merchPageModifyVo.getMerchType());

        entityXfMerchantInfo.setMerchantUsername(merchPageModifyVo.getMerchantUsername());
        entityXfMerchantInfo.setMerchantPhone(merchPageModifyVo.getMerchantPhone());

        //添加操作
//        if( CommonUtil.isNull(entityXfMerchantInfo1.getCompanyId())){
//            entityXfMerchantInfo.setUserName(RandomUtil.getRandomString(8));
//            entityXfMerchantInfo.setPassword(RandomUtil.getRandomString(8));
//
//            //2 统一支付平台发起请求
//            PayCenterCompanyVo payCenterCompanyVo=new PayCenterCompanyVo();
//            payCenterCompanyVo.setCompanyName(merchPageModifyVo.getMerchName());
//            payCenterCompanyVo.setUserName(entityXfMerchantInfo.getUserName());
//            payCenterCompanyVo.setPayPassword(entityXfMerchantInfo.getPassword());
//            payCenterCompanyVo.setPayChannel("00000000000000000000");
//            payCenterCompanyVo.setIsEverySet(merchPageModifyVo.getIsEverySet());
//            payCenterCompanyVo.setLocked(merchPageModifyVo.getLocked());
//            payCenterCompanyVo.setContactName(merchPageModifyVo.getContactName());
//            payCenterCompanyVo.setContactNumber(merchPageModifyVo.getContactNumber());
//            payCenterCompanyVo.setContactAddress(merchPageModifyVo.getContactAddress());
//
//            PayCenterVo payCenterVo=payCenterService.addMerch(payCenterCompanyVo);
//            if(payCenterVo==null || !payCenterVo.getCode().equals("0000")){
//                return ResultUtil.error(ResultEnum.Err_2018);
//            }
//            Map<String,String> map = JSON.parseObject(JSON.toJSONString(payCenterVo.getData()), Map.class);
//
//            entityXfMerchantInfo.setCompanyId(map.get("companyId"));
//        }else{
            //2 统一支付平台发起请求
//            PayCenterCompanyVo payCenterCompanyVo=new PayCenterCompanyVo();
//            payCenterCompanyVo.setCompanyId(entityXfMerchantInfo1.getCompanyId());
//            payCenterCompanyVo.setCompanyName(entityCompanyList.getComName()+"#"+merchPageModifyVo.getMerchName());
//            payCenterCompanyVo.setPayChannel("00000000000000000000");
//            payCenterCompanyVo.setIsEverySet(merchPageModifyVo.getIsEverySet());
//            payCenterCompanyVo.setLocked(merchPageModifyVo.getLocked());
//            payCenterCompanyVo.setContactName(merchPageModifyVo.getContactName());
//            payCenterCompanyVo.setContactNumber(merchPageModifyVo.getContactNumber());
//            payCenterCompanyVo.setContactAddress(merchPageModifyVo.getContactAddress());

//            PayCenterVo payCenterVo=payCenterService.editMerch(payCenterCompanyVo);
//            if(payCenterVo==null || !payCenterVo.getCode().equals("0000")){
//                return ResultUtil.error(ResultEnum.Err_2018);
//            }
//        }

        //TODO 等统一平台确认修改成功

        return payConfigClientService.UpdateMerchInfo(entityXfMerchantInfo);
    }

    /**
     * 餐段删除（MealPage/delete）
     * */
    @Override
    public Result MealPageDelete(MealPageDeleteVo mealPageDeleteVo) {
        Map<String, Object> map = new HashMap<>();
        map.put("comId",mealPageDeleteVo.getComId());
        map.put("mealNo",mealPageDeleteVo.getMealNo());
        map.put("id",mealPageDeleteVo.getMealId());
        payConfigClientService.DeleteMealNameInfo(map);
        map.put("id",mealPageDeleteVo.getId());
        payConfigClientService.DeleteMealTimeInfo(map);
        return ResultUtil.success();
    }


}
